{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "27154e57",
   "metadata": {},
   "source": [
    "# 列表的切片\n",
    "* 课本P67、P81练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "0326d493",
   "metadata": {},
   "outputs": [],
   "source": [
    "phrase = \"Don't panic!\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "cc54516c",
   "metadata": {},
   "outputs": [],
   "source": [
    "plist = list(phrase)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "805e83d8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n"
     ]
    }
   ],
   "source": [
    "print(phrase)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "b26679c6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n"
     ]
    }
   ],
   "source": [
    "print(plist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "79b670c2",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(4):\n",
    "    plist.pop()\n",
    "plist.pop(0)\n",
    "plist.remove(\"'\")\n",
    "plist.extend([plist.pop(),plist.pop()])\n",
    "plist.insert(2,plist.pop(3))\n",
    "new_phrase = ''.join(plist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "0654db2e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['o', 'n', ' ', 't', 'a', 'p']\n"
     ]
    }
   ],
   "source": [
    "print(plist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "c3cf211f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "on tap\n"
     ]
    }
   ],
   "source": [
    "print(new_phrase)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "41f19a21",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "527794a6",
   "metadata": {},
   "outputs": [],
   "source": [
    "phrase = \"Don't panic!\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "eae21214",
   "metadata": {},
   "outputs": [],
   "source": [
    "plist = list(phrase)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "e55ecc0c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n"
     ]
    }
   ],
   "source": [
    "print(phrase)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "e3483353",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n"
     ]
    }
   ],
   "source": [
    "print(plist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "d0e3b523",
   "metadata": {},
   "outputs": [],
   "source": [
    "new_phrase = ''.join(plist[1:3])\n",
    "new_phrase = new_phrase + ''.join([plist[5],plist[4],plist[7],plist[6]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "6956b7f3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n"
     ]
    }
   ],
   "source": [
    "print(plist)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "d5e3d447",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "on tap\n"
     ]
    }
   ],
   "source": [
    "print(new_phrase)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e9380392",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "8f80be38",
   "metadata": {},
   "source": [
    "# 总结列表\n",
    "* 列表是一个有序且可更改的集合。在 Python 中，列表用方括号编写。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8077f5be",
   "metadata": {},
   "source": [
    "* 创建列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "25ba9cb9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['黄赛', '刘石珍', '杨静雯', '李彤清', '黄蕾']\n"
     ]
    }
   ],
   "source": [
    "names = [\"黄赛\",\"刘石珍\",\"杨静雯\",\"李彤清\",\"黄蕾\"]\n",
    "print(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c22ac324",
   "metadata": {},
   "source": [
    "* 通过索引来打印第二项"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c711e375",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "刘石珍\n"
     ]
    }
   ],
   "source": [
    "print(names[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "985dd2da",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['刘石珍', '杨静雯', '李彤清']\n"
     ]
    }
   ],
   "source": [
    "print(names[1:4])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8537dec7",
   "metadata": {},
   "source": [
    "* 从索引 -4（包括）到索引 -1（排除）的项目"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "6849c94e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['刘石珍', '杨静雯', '李彤清']\n"
     ]
    }
   ],
   "source": [
    "print(names[-4:-1])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e5ddbff",
   "metadata": {},
   "source": [
    "* 更改项目值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "dde6edd9",
   "metadata": {},
   "outputs": [],
   "source": [
    "names[1] = \"刘文燕\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "634fdec6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['黄赛', '刘文燕', '杨静雯', '李彤清', '黄蕾']\n"
     ]
    }
   ],
   "source": [
    "print(names)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "df3873f6",
   "metadata": {},
   "source": [
    "* 检查项目是否存在"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "272eb6e7",
   "metadata": {},
   "outputs": [],
   "source": [
    "names = [\"黄赛\",\"刘石珍\",\"杨静雯\",\"李彤清\",\"黄蕾\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "42250f8a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "yes!我在\n"
     ]
    }
   ],
   "source": [
    "if \"黄赛\" in names:\n",
    "    print(\"yes!我在\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b8d7d572",
   "metadata": {},
   "source": [
    "* 列表长度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "27e70f94",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ab1a6125",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
